home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-installer.exe / bin / quodlibet / qltk / delete.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  9KB  |  247 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. '''
  5. Functions for deleting files and songs with user interaction.
  6.  
  7. Only use trash_files() or trash_songs() and TrashMenuItem().
  8. '''
  9. import os
  10. from gi.repository import Gtk
  11. from quodlibet.util import trash
  12. from quodlibet.qltk import get_top_parent
  13. from quodlibet.qltk.msg import ErrorMessage, WarningMessage
  14. from quodlibet.qltk.wlw import WaitLoadWindow
  15. from quodlibet.qltk.x import Button, MenuItem, Alignment
  16. from quodlibet.util.path import fsdecode, unexpand
  17.  
  18. class FileListExpander(Gtk.Expander):
  19.     '''A widget for showing a static list of file paths'''
  20.     
  21.     def __init__(self, paths):
  22.         super(FileListExpander, self).__init__(label = _('Files:'))
  23.         self.set_resize_toplevel(True)
  24.         paths = [ fsdecode(unexpand(p)) for p in paths ]
  25.         lab = Gtk.Label(label = '\n'.join(paths))
  26.         lab.set_alignment(0, 0)
  27.         lab.set_selectable(True)
  28.         win = Gtk.ScrolledWindow()
  29.         win.add_with_viewport(Alignment(lab, border = 6))
  30.         win.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
  31.         win.set_shadow_type(Gtk.ShadowType.ETCHED_OUT)
  32.         win.set_size_request(-1, 100)
  33.         self.add(win)
  34.         win.show_all()
  35.  
  36.  
  37.  
  38. class DeleteDialog(WarningMessage):
  39.     RESPONSE_DELETE = 1
  40.     
  41.     def for_songs(cls, parent, songs):
  42.         '''Create a delete dialog for deleting songs'''
  43.         description = _('The selected songs will be removed from the library and their files deleted from disk.')
  44.         paths = [ s('~filename') for s in songs ]
  45.         return cls(parent, paths, description)
  46.  
  47.     for_songs = classmethod(for_songs)
  48.     
  49.     def for_files(cls, parent, paths):
  50.         '''Create a delete dialog for deleting files'''
  51.         description = _('The selected files will be deleted from disk.')
  52.         return cls(parent, paths, description)
  53.  
  54.     for_files = classmethod(for_files)
  55.     
  56.     def __init__(self, parent, paths, description):
  57.         title = ngettext('Delete %(file_count)d file permanently?', 'Delete %(file_count)d files permanently?', len(paths)) % {
  58.             'file_count': len(paths) }
  59.         super(DeleteDialog, self).__init__(get_top_parent(parent), title, description, buttons = Gtk.ButtonsType.NONE)
  60.         area = self.get_message_area()
  61.         exp = FileListExpander(paths)
  62.         exp.show()
  63.         area.pack_start(exp, False, True, 0)
  64.         self.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)
  65.         delete_button = Button(_('_Delete Files'), Gtk.STOCK_DELETE)
  66.         delete_button.show()
  67.         self.add_action_widget(delete_button, self.RESPONSE_DELETE)
  68.         self.set_default_response(Gtk.ResponseType.CANCEL)
  69.  
  70.  
  71.  
  72. class TrashDialog(WarningMessage):
  73.     RESPONSE_TRASH = 1
  74.     
  75.     def for_songs(cls, parent, songs):
  76.         '''Create a trash dialog for trashing songs'''
  77.         description = _('The selected songs will be removed from the library and their files moved to the trash.')
  78.         paths = [ s('~filename') for s in songs ]
  79.         return cls(parent, paths, description)
  80.  
  81.     for_songs = classmethod(for_songs)
  82.     
  83.     def for_files(cls, parent, paths):
  84.         '''Create a trash dialog for trashing files'''
  85.         description = _('The selected files will be moved to the trash.')
  86.         return cls(parent, paths, description)
  87.  
  88.     for_files = classmethod(for_files)
  89.     
  90.     def __init__(self, parent, paths, description):
  91.         title = ngettext('Move %(file_count)d file to the trash?', 'Move %(file_count)d files to the trash?', len(paths)) % {
  92.             'file_count': len(paths) }
  93.         super(TrashDialog, self).__init__(get_top_parent(parent), title, description, buttons = Gtk.ButtonsType.NONE)
  94.         area = self.get_message_area()
  95.         exp = FileListExpander(paths)
  96.         exp.show()
  97.         area.pack_start(exp, False, True, 0)
  98.         self.add_button(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL)
  99.         trash_button = Button(_('_Move to Trash'), 'user-trash')
  100.         trash_button.show()
  101.         self.add_action_widget(trash_button, self.RESPONSE_TRASH)
  102.         self.set_default_response(Gtk.ResponseType.CANCEL)
  103.  
  104.  
  105.  
  106. def TrashMenuItem():
  107.     if trash.can_trash():
  108.         return MenuItem(_('_Move to Trash'), 'user-trash')
  109.     return None.ImageMenuItem(Gtk.STOCK_DELETE, use_stock = True)
  110.  
  111.  
  112. def _do_trash_songs(parent, songs, librarian):
  113.     dialog = TrashDialog.for_songs(parent, songs)
  114.     resp = dialog.run()
  115.     if resp != TrashDialog.RESPONSE_TRASH:
  116.         return None
  117.     window_title = None('Moving %(current)d/%(total)d.')
  118.     w = WaitLoadWindow(parent, len(songs), window_title)
  119.     w.show()
  120.     ok = []
  121.     failed = []
  122.     for song in songs:
  123.         filename = song('~filename')
  124.         
  125.         try:
  126.             trash.trash(filename)
  127.         except trash.TrashError:
  128.             failed.append(song)
  129.  
  130.         ok.append(song)
  131.         w.step()
  132.     
  133.     w.destroy()
  134.     if failed:
  135.         ErrorMessage(parent, _('Unable to move to trash'), _('Moving one or more files to the trash failed.')).run()
  136.     if ok:
  137.         librarian.remove(ok)
  138.  
  139.  
  140. def _do_trash_files(parent, paths):
  141.     dialog = TrashDialog.for_files(parent, paths)
  142.     resp = dialog.run()
  143.     if resp != TrashDialog.RESPONSE_TRASH:
  144.         return None
  145.     window_title = None('Moving %(current)d/%(total)d.')
  146.     w = WaitLoadWindow(parent, len(paths), window_title)
  147.     w.show()
  148.     ok = []
  149.     failed = []
  150.     for path in paths:
  151.         
  152.         try:
  153.             trash.trash(path)
  154.         except trash.TrashError:
  155.             failed.append(path)
  156.  
  157.         ok.append(path)
  158.         w.step()
  159.     
  160.     w.destroy()
  161.     if failed:
  162.         ErrorMessage(parent, _('Unable to move to trash'), _('Moving one or more files to the trash failed.')).run()
  163.  
  164.  
  165. def _do_delete_songs(parent, songs, librarian):
  166.     dialog = DeleteDialog.for_songs(parent, songs)
  167.     resp = dialog.run()
  168.     if resp != DeleteDialog.RESPONSE_DELETE:
  169.         return None
  170.     window_title = None('Deleting %(current)d/%(total)d.')
  171.     w = WaitLoadWindow(parent, len(songs), window_title)
  172.     w.show()
  173.     ok = []
  174.     failed = []
  175.     for song in songs:
  176.         filename = song('~filename')
  177.         
  178.         try:
  179.             os.unlink(filename)
  180.         except EnvironmentError:
  181.             failed.append(song)
  182.  
  183.         ok.append(song)
  184.         w.step()
  185.     
  186.     w.destroy()
  187.     if failed:
  188.         ErrorMessage(parent, _('Unable to delete files'), _('Deleting one or more files failed.')).run()
  189.     if ok:
  190.         librarian.remove(ok)
  191.  
  192.  
  193. def _do_delete_files(parent, paths):
  194.     dialog = DeleteDialog.for_files(parent, paths)
  195.     resp = dialog.run()
  196.     if resp != DeleteDialog.RESPONSE_DELETE:
  197.         return None
  198.     window_title = None('Deleting %(current)d/%(total)d.')
  199.     w = WaitLoadWindow(parent, len(paths), window_title)
  200.     w.show()
  201.     ok = []
  202.     failed = []
  203.     for path in paths:
  204.         
  205.         try:
  206.             os.unlink(path)
  207.         except EnvironmentError:
  208.             failed.append(path)
  209.  
  210.         ok.append(path)
  211.         w.step()
  212.     
  213.     w.destroy()
  214.     if failed:
  215.         ErrorMessage(parent, _('Unable to delete files'), _('Deleting one or more files failed.')).run()
  216.  
  217.  
  218. def trash_files(parent, paths):
  219.     '''Will try to move the files to the trash,
  220.     or if not possible, delete them permanently.
  221.  
  222.     Will ask for confirmation in each case.
  223.     '''
  224.     if not paths:
  225.         return None
  226.     if None.can_trash():
  227.         _do_trash_files(parent, paths)
  228.     else:
  229.         _do_delete_files(parent, paths)
  230.  
  231.  
  232. def trash_songs(parent, songs, librarian):
  233.     '''Will try to move the files associated with the songs to the trash,
  234.     or if not possible, delete them permanently.
  235.  
  236.     Will ask for confirmation in each case.
  237.  
  238.     The deleted songs will be removed from the librarian.
  239.     '''
  240.     if not songs:
  241.         return None
  242.     if None.can_trash():
  243.         _do_trash_songs(parent, songs, librarian)
  244.     else:
  245.         _do_delete_songs(parent, songs, librarian)
  246.  
  247.